********************************************************************************
**  TITLE:      pt2009_pvs_contact_networks                                   **  
**  AUTHOR:	    Philippe Mongrain                                             **
**  DATA:       ElectorsSurvey_PT_2008_db_0                                   **
**  DATE:	    October 2022 						                          **	
**  VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "pt2009_pvs_contact_networks", replace

* Open the dataset

import spss using "ElectorsSurvey_PT_2008_db_0.sav", clear case(lower)


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

destring data, gen(sdate)

gen surveydate=date(string(sdate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY surveydate

gen edate = 20090927

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

gen time = (electiondate - surveydate) - 1 // Substract 1 to correct for election date

* Gender

gen male = ccs_q54

recode male (2=0)

* Age

gen age = ccs_q55_anos

replace age = . if age < 17

* Education

gen education = evs_q59

recode education (10=.)
recode education (99=.)

* Interest for politics

gen interest = evs_l37

* Party identification

gen pidstatus_whole = .

replace pidstatus_whole = 1 if ees_e17!=4 & ees_e17 < 97 // Loser PID
replace pidstatus_whole = 2 if ees_e17 == 7 // No PID
replace pidstatus_whole = 3 if ees_e17 == 4 // Winner PID

label define pidstatus 1 "Loser PID" 2 "No PID" 3 "Winner PID" 
label values pidstatus_whole pidstatus

* Party identification scale

gen pidscale_whole = .

replace pidscale_whole = 1 if pidstatus_whole == 1 & ees_e18 == 1
replace pidscale_whole = 2 if pidstatus_whole == 1 & ees_e18 == 2
replace pidscale_whole = 3 if pidstatus_whole == 1 & ees_e18 == 3
replace pidscale_whole = 4 if pidstatus_whole == 2
replace pidscale_whole = 5 if pidstatus_whole == 3 & ees_e18 == 3
replace pidscale_whole = 6 if pidstatus_whole == 3 & ees_e18 == 2
replace pidscale_whole = 7 if pidstatus_whole == 3 & ees_e18 == 1

label define pidscale 1 "Strong loser PID" 2 "Moderate loser PID" 3 "Weak loser PID" 4 "No PID" 5 "Weak winner PID" 6 "Moderate winner PID" 7 "Strong winner PID" 
label values pidscale_whole pidscale

* Press news attentiveness

gen press = evs_p53_1

recode press (.=-99)

* Radio news attentiveness

gen radio = evs_p53_2

recode radio (.=-99)

* TV news attentiveness

gen tv = evs_p53_3

recode tv (.=-99)

* Internet news attentiveness

gen internet = evs_p53_4

recode internet (.=-99)

* News attentiveness index

egen news = rowmax(press radio tv internet)

recode news (-99=.)

* Political discussion

gen discussion = evs_l38 - 1


**********************************************
** NATIONAL-LEVEL EXPECTATIONS (MORE VOTES) **
**********************************************

* Forecasts

gen forecast_morevotes = ceapp_p05_n49

* Identify correct forecasts

gen correct_morevotes_d = 0 if forecast_morevotes!=2 & forecast_morevotes!=.
replace correct_morevotes_d = 1 if forecast_morevotes == 2


*****************************************************
** NATIONAL-LEVEL EXPECTATIONS (ABSOLUTE MAJORITY) **
*****************************************************

* Forecasts

gen forecasts_majority = cesop_n49_1

* Identify correct forecasts

gen correct_majority_d = 0 if forecasts_majority!=1 & forecasts_majority!=. & correct_morevotes_d == 1
replace correct_majority_d = 1 if forecasts_majority == 1 & correct_morevotes_d == 1
replace correct_majority_d = . if correct_morevotes_d == 0 | correct_morevotes_d == .


**********
** SAVE **
**********

save "pt2009_pvs.dta", replace


*******************
** MAIN ANALYSES **
*******************

* Regression analysis and percentage of correct and incorrect forecasts (more votes)

logistic correct_morevotes_d discussion pidscale_whole interest news age i.male education time
estimates table, star(.05 .01 .001)

tab correct_morevotes_d if e(sample) == 1

* Regression analysis and percentage of correct and incorrect forecasts (absolute majority)

logistic correct_majority_d discussion pidscale_whole interest news age i.male education time
estimates table, star(.05 .01 .001)

tab correct_majority_d if e(sample) == 1


************************
** SUMMARY STATISITCS **
************************

label variable correct_morevotes_d "More votes (DV)"
label variable correct_majority_d "Seat majority (DV)"
label variable discussion "Discussion"
label variable pidscale_whole "National PID"
label variable interest "Interest"
label variable news "News attentiveness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"

quietly logistic correct_morevotes_d discussion pidscale_whole interest news age i.male education time

estpost tabstat correct_morevotes_d discussion pidscale_whole interest news age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "pt2009_pvs_summary_national_morevotes.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2009 Portuguese legislative election (CIES-IUL) (more votes), national level") replace

eststo clear

quietly logistic correct_majority_d discussion pidscale_whole interest news age i.male education time

estpost tabstat correct_majority_d discussion pidscale_whole interest news age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "pt2009_pvs_summary_national_majority.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2009 Portuguese legislative election (CIES-IUL) (absolute majority), national level") replace

eststo clear

log close